"""
@name : shopping.tools
@author : ysy
@projectname: tlbb
"""

# 定义用户
user_list = {"root":{"password":"123456",
                     "balance":"10"},
             "hahyyy":{"password":"123",
                     "balance":"20"}}

# 定义商品
goods_list = {"F00001":{"name":"苹果",
                        "price":2},
              "F00002":{"name":"香蕉",
                        "price":3},
              "F00003":{"name":"葡萄",
                        "price":4},
              "F00004":{"name":"梨子",
                        "price":5}}


def show_menu():
    print("*" * 50)
    print("欢迎登录【三乐购物系统】！")
    print("1.登录")
    print("2.注册")
    print("3.查看商品")
    print("4.购买商品")
    print("")
    print("exit.退出")
    print("*" * 50)


def user_login():
    global  username,password
    username = input("请输入您的用户名：")
    if username in user_list.keys():
        password = input("请输入您的密码：")
        if password == user_list[username]["password"]:
            print("登录成功！")
            print(f"您的余额还有{user_list[username]['balance']}")
        else :
            print("密码错误，请重新输入！")
            return user_login()
    else:
        print("您输入的用户名不存在，请注册")


def user_register():
    global  name_register,passwd_register,balance_register
    # 提示用户输入信息
    name_register = input("请输入用户名：")
    if name_register in user_list.keys():
        print("用户名已存在，请重新输入！")
        return user_register()
    else:
        passwd_register = input("请输入密码：")
        balance_register = input("请输入充值金额：")

    # 创建一个新字典保存新注册的信息
    register_dict = {name_register: {'password': passwd_register, 'balance': balance_register}}

    # 将新注册的用户信息加入到原字典中
    user_list.update(register_dict)

    # 提示用户添加成功
    print("注册成功，请登录！")


def show_goods():
    print("-" * 50)
    print("今日商品".center(48))
    print("编号：F00001 苹果 2元".center(50))
    print("编号：F00002 香蕉 3元".center(50))
    print("编号：F00003 葡萄 4元".center(50))
    print("编号：F00004 梨子 5元".center(50))
    print("-" * 50)


def buy_goods():
    global user_buy,nums_buy,balance,amount_buy,balance_outstanding,continue_buy
    user_buy = input("请输入需要购买物品的编号：")
    if user_buy in goods_list.keys():
        nums_buy = input("请问需要买几份：")
        balance = int(user_list[username]['balance'])
        amount_buy = int(nums_buy) * goods_list[user_buy]['price']
        print(f"总价格为{amount_buy}，您的余额为{balance}")
        if amount_buy <= balance:
            balance_outstanding = balance - amount_buy
            print(f"您的余额足够购买，以下单，您的余额还有{balance_outstanding}")
        else :
            print("您的余额不够，请充值")
    else:
        print("已售罄，请改天再来！")
        return buy_goods()








